package cn.ysnk.clz.controller.adminMenu;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.jfinal.core.Controller;

import cn.ysnk.clz.config.annotation.ControllerAnnotation;
import cn.ysnk.clz.entity.menu.AdminMenu;
import cn.ysnk.clz.entity.menu.AdminSubMenu;
import cn.ysnk.clz.interceptor.security.role.RoleEnum;

@ControllerAnnotation(url="/adminMenu",roleType={RoleEnum.ROLE_ADMIN,RoleEnum.ROLE_MANAGER,RoleEnum.ROLE_USER})
public class AdminMenuController extends Controller {
	public void index() {
		List<AdminMenu> menuList = AdminMenu.adminMenuDao.findAllMenu();
		setAttr("menuList", menuList);
		renderJsp("/admin/menu/adminMenu.jsp");
	}
	
	/**
	 * 菜单列表界面
	 */
	public void manage(){
		List<AdminMenu> menuList = AdminMenu.adminMenuDao.findAllMenu();
		setAttr("menuList", menuList);
		renderJsp("/admin/menu/adminMenuList.jsp");
	}
	
	/**
	 * 编辑或者新增菜单
	 */
	public void editMenu(){
		String editMenuId = getPara("menuId");
		if(editMenuId!=null&&!"".equals(editMenuId))
			setAttr("menuId", editMenuId);
		renderJsp("/admin/menu/editMenu.jsp");
	}
	
	public void saveMenu(){
		String menuId = getPara("menuId");
		String menuname = getPara("menuname");
		String iconName = getPara("iconName");
		int sort = getParaToInt("sort");
		AdminMenu menu = null;
		if("".equals(menuId)){
			menu = new AdminMenu();
			menu.set("id", UUID.randomUUID().toString());
			menu.set("addTime", new Date());
		}else{
			menu = AdminMenu.adminMenuDao.findById(menuId);
		}
		menu.set("name", menuname);
		menu.set("icon", iconName);
		menu.set("sort", sort);
		menu.set("updateTime", new Date());
		boolean isSuccess = false;
		if("".equals(menuId)){
			isSuccess = menu.save();
		}else{
			isSuccess = menu.update();
		}
		if(isSuccess){
			renderJson("{\"error\":0,\"message\":\"\"}");
		}else{
			renderJson("{\"error\":1,\"message\":\"操作失败，请重试！\"}");
		}
	}
	
	public void deleteMenu(){
		String menuId = getPara("menuId");
		AdminMenu menu = AdminMenu.adminMenuDao.findById(menuId);
		if(menu.getSubMenu().size()>0){
			renderJson("{\"error\":1,\"message\":\"存在子菜单，请先删除子菜单！\"}");
		}else{
			if(AdminMenu.adminMenuDao.deleteById(menuId)){
				renderJson("{\"error\":0,\"message\":\"\"}");
			}else{
				renderJson("{\"error\":1,\"message\":\"操作失败，请重试！\"}");
			}
		}
	}
	
	
	public void saveSubMenu(){
		String menuId = getPara("menuId");
		String subMenuId = getPara("subMenuId");
		String menuname = getPara("menuname");
		String iconName = getPara("iconName");
		String menuAction = getPara("menuAction");
		int sort = getParaToInt("sort");
		AdminSubMenu menu = null;
		if("".equals(subMenuId)){
			menu = new AdminSubMenu();
			menu.set("id", UUID.randomUUID().toString());
			menu.set("addTime", new Date());
		}else{
			menu = AdminSubMenu.adminSubMenuDao.findById(subMenuId);
		}
		menu.set("menuId", menuId);
		menu.set("name", menuname);
		menu.set("icon", iconName);
		menu.set("sort", sort);
		menu.set("action", menuAction);
		menu.set("updateTime", new Date());
		boolean isSuccess = false;
		if("".equals(subMenuId)){
			isSuccess = menu.save();
		}else{
			isSuccess = menu.update();
		}
		if(isSuccess){
			renderJson("{\"error\":0,\"message\":\"\"}");
		}else{
			renderJson("{\"error\":1,\"message\":\"操作失败，请重试！\"}");
		}
	}
	
	public void deleteSubMenu(){
		String subMenuId = getPara("subMenuId");
		if(AdminSubMenu.adminSubMenuDao.deleteById(subMenuId)){
			renderJson("{\"error\":0,\"message\":\"\"}");
		}else{
			renderJson("{\"error\":1,\"message\":\"操作失败，请重试！\"}");
		}
	}
	
	/**
	 * 子菜单弹出层
	 */
	public void subMenuList(){
		String menuId = getPara("menuId");
		List<AdminSubMenu> list = AdminSubMenu.adminSubMenuDao.findSubMenuByParentMenuId(menuId);
		setAttr("list", list);
		setAttr("menuId",menuId);
		renderJsp("/admin/menu/subMenuList.jsp");
	}
	
	/**
	 * 编辑或者新增菜单 
	 */
	public void subEditMenu(){
		String editMenuId = getPara("menuId");
		String subMenuId = getPara("subMenuId");
		if(subMenuId!=null&&!"".equals(subMenuId))
			setAttr("subMenuId", subMenuId);
		setAttr("menuId", editMenuId);
		renderJsp("/admin/menu/editSubMenu.jsp");
	}
	
}
